package com.xckj.zuoliao.view;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewTreeObserver;
import android.view.WindowManager;
import android.widget.TextView;

import androidx.appcompat.app.ActionBar;

import com.xckj.zuoliao.R;

/**
 * @Author: 代码的搬运工
 * @Describe: 自定义Dialog
 * @Date: 2024/4/1 18:32
 */
public class CustomDialogView {
    private final Context context;
    private final int themeResId;
    private final View layout;
    private boolean cancelable = true;
    private CharSequence title, message, cancelText, sureText;
    private View.OnClickListener sureClickListener, cancelClickListener;
    private boolean isOneButton = false;

    public CustomDialogView(Context context) {
        this(context, R.style.CustomDialog);
    }

    @SuppressLint("InflateParams")
    public CustomDialogView(Context context, int themeResId) {
        this(context, themeResId, ((LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.view_custom_dialog, null));
    }

    /**
     * 自定义layout用这个
     */
    public CustomDialogView(Context context, int themeResId, View layout) {
        this.context = context;
        this.themeResId = themeResId;
        this.layout = layout;
    }


    /**
     * 能否返回键取消
     */
    public CustomDialogView setCancelable(Boolean cancelable) {
        this.cancelable = cancelable;
        return this;
    }

    public CustomDialogView setTitle(CharSequence title) {
        this.title = title;
        return this;
    }

    public CustomDialogView setMessage(CharSequence message) {
        this.message = message;
        return this;
    }

    public CustomDialogView setCancelText(CharSequence str) {
        this.cancelText = str;
        return this;
    }


    /**
     * 确定按钮文本
     */
    public CustomDialogView setSureText(CharSequence str) {
        this.sureText = str;
        return this;
    }

    public CustomDialogView setSureOnClickListener(View.OnClickListener listener) {
        this.sureClickListener = listener;
        return this;
    }

    public CustomDialogView setCancelOnClickListener(View.OnClickListener listener) {
        this.cancelClickListener = listener;
        return this;
    }

    public Dialog build() {
        final Dialog dialog = new Dialog(context, themeResId);
        dialog.setCancelable(cancelable);
        dialog.addContentView(layout, new ActionBar.LayoutParams(ActionBar.LayoutParams.MATCH_PARENT, ActionBar.LayoutParams.WRAP_CONTENT));
        /*设置显不显示*/
        setText(title, R.id.tv_title);
        setText(message, R.id.tv_message);
        setText(cancelText, R.id.tv_cancel);
        setText(sureText, R.id.tv_sure);
        if (isValid(cancelText) && isValid(sureText)) {
            layout.findViewById(R.id.view_line).setVisibility(View.VISIBLE);
        }
        if (isOneButton) {
            layout.findViewById(R.id.tv_cancel).setVisibility(View.GONE);
        } else {
            layout.findViewById(R.id.tv_cancel).setVisibility(View.VISIBLE);
        }
        /*一行居中*/
        final TextView textView = (TextView) layout.findViewById(R.id.tv_message);
        textView.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
            @Override
            public boolean onPreDraw() {
                if (textView.getLineCount() == 1) {
                    textView.setGravity(Gravity.CENTER);
                }
                return true;
            }
        });
        /*设置点击监听*/
        if (sureClickListener != null) {
            layout.findViewById(R.id.tv_sure).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    sureClickListener.onClick(view);
                    dialog.dismiss();
                }
            });
        }
        if (cancelClickListener != null) {
            layout.findViewById(R.id.tv_cancel).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    cancelClickListener.onClick(view);
                    dialog.dismiss();
                }
            });
        }
        /*设置宽度*/
        WindowManager.LayoutParams params = dialog.getWindow().getAttributes();
        params.width = (int) (context.getResources().getDisplayMetrics().widthPixels * 0.80);
        dialog.getWindow().setAttributes(params);
        return dialog;
    }

    private void setText(CharSequence text, int id) {
        if (isValid(text)) {
            TextView textView = (TextView) layout.findViewById(id);
            textView.setText(text);
            textView.setVisibility(View.VISIBLE);
        }
    }

    private boolean isValid(CharSequence text) {
        return text != null && !"".equals(text.toString().trim());
    }

    public CustomDialogView showOneButton(boolean isOneButton) {
        this.isOneButton = isOneButton;
        return this;
    }
}
